- page_title "#{@build.name} (##{@build.id})", "构建"
= render "header_title"
- trace_with_state = @build.trace_with_state

.build-page
  .row-content-block.top-block
    从
    = link_to @build.ref, namespace_project_commits_path(@project.namespace, @project, @build.ref)
    提交的构建 ##{@build.id}
    %strong.monospace= link_to @build.commit.short_sha, ci_status_path(@build.commit)
    - merge_request = @build.merge_request
    - if merge_request
      来自于
      = link_to "合并请求 #{merge_request.to_reference}", merge_request_path(merge_request)

  #up-build-trace
  - builds = @build.commit.builds.latest.to_a
  - if builds.size > 1
    %ul.nav-links.no-top.no-bottom
      - builds.each do |build|
        %li{class: ('active' if build == @build) }
          = link_to namespace_project_build_path(@project.namespace, @project, build) do
            = ci_icon_for_status(build.status)
            %span
              - if build.name
                = build.name
              - else
                = build.id

      - if @build.retried?
        %li.active
          %a
            构建 ##{@build.id}
            &middot;
            %i.fa.fa-warning
            此构建已重试。

  .row-content-block.middle-block
    .build-head
      .clearfix
        = ci_status_with_icon(@build.status)
        - if @build.duration
          %span
            %i.fa.fa-time
            #{duration_in_words(@build.finished_at, @build.started_at)}
        .pull-right
          #{time_ago_with_tooltip(@build.finished_at) if @build.finished_at}

  - if @build.stuck?
    - unless @build.any_runners_online?
      .bs-callout.bs-callout-warning
        %p
          - if no_runners_for_project?(@build.project)
            此构建被卡住，因为没有 runner 指定给该项目。
          - elsif @build.tags.any?
            此构建被卡住，因为没有指定标签的 runner 指定给该项目。指定的标签如下：
            - @build.tags.each do |tag|
              %span.label.label-primary
                = tag
          - else
            此构建被卡住，因为没有可用的 runner 来运行此构建。

          %br
          转到
          = link_to namespace_project_runners_path(@build.project.namespace, @build.project) do
            Runners 页面

  .row.prepend-top-default
    .col-md-9
      .clearfix
        - if @build.active?
          .autoscroll-container
            %button.btn.btn-success.btn-sm#autoscroll-button{:type => "button", :data => {:state => 'disabled'}} 开启自动滚动
          .clearfix
      #js-build-scroll.scroll-controls
        = link_to '#up-build-trace', class: 'btn' do
          %i.fa.fa-angle-up
        = link_to '#down-build-trace', class: 'btn' do
          %i.fa.fa-angle-down

      - if @build.erased?
        .erased.alert.alert-warning
          - erased_by = "被 #{link_to @build.erased_by.name, user_path(@build.erased_by)}" if @build.erased_by
          构建#{erased_by.html_safe} 在 #{time_ago_with_tooltip(@build.erased_at)}删除
      - else
        %pre.trace#build-trace
          %code.bash
            = preserve do
              = raw trace_with_state[:html]
              - if @build.active?
                %i{:class => "fa fa-refresh fa-spin"}

      %div#down-build-trace

    .col-md-3
      - if @build.coverage
        .build-widget
          %h4.title
            测试覆盖
          %h1 #{@build.coverage}%

      - if can?(current_user, :read_build, @project) && @build.artifacts?
        .build-widget.artifacts
          %h4.title 构建附件
          .center
            .btn-group{ role: :group }
              = link_to download_namespace_project_build_artifacts_path(@project.namespace, @project, @build), class: 'btn btn-sm btn-primary' do
                = icon('download')
                下载

              - if @build.artifacts_metadata?
                = link_to browse_namespace_project_build_artifacts_path(@project.namespace, @project, @build), class: 'btn btn-sm btn-primary' do
                  = icon('folder-open')
                  浏览

      .build-widget.build-controls
        %h4.title
          构建 ##{@build.id}
          - if can?(current_user, :update_build, @project)
            .center
              .btn-group{ role: :group }
                - if @build.active?
                  = link_to "取消", cancel_namespace_project_build_path(@project.namespace, @project, @build), class: 'btn btn-sm btn-danger', method: :post
                - elsif @build.retryable?
                  = link_to "重试", retry_namespace_project_build_path(@project.namespace, @project, @build), class: 'btn btn-sm btn-primary', method: :post

                - if @build.erasable?
                  = link_to erase_namespace_project_build_path(@project.namespace, @project, @build),
                            class: 'btn btn-sm btn-warning', method: :post,
                            data: { confirm: '确定要删除此构建么？' } do
                    = icon('eraser')
                    删除
                - if @build.has_trace?
                  = link_to '原文件', raw_namespace_project_build_path(@project.namespace, @project, @build),
                            class: 'btn btn-sm btn-success', target: '_blank'

        .clearfix
          - if @build.duration
            %p
              %span.attr-name 运行时间：
              #{duration_in_words(@build.finished_at, @build.started_at)}
          %p
            %span.attr-name 创建时间：
            #{time_ago_with_tooltip(@build.created_at)}
          - if @build.finished_at
            %p
              %span.attr-name 结束时间：
              #{time_ago_with_tooltip(@build.finished_at)}
          - if @build.erased_at
            %p
              %span.attr-name 删除时间：
              #{time_ago_with_tooltip(@build.erased_at)}
          %p
            %span.attr-name Runner：
            - if @build.runner && current_user && current_user.admin
              = link_to "##{@build.runner.id}", admin_runner_path(@build.runner.id)
            - elsif @build.runner
              \##{@build.runner.id}

      - if @build.trigger_request
        .build-widget
          %h4.title
            触发

          %p
            %span.attr-name 授权码：
            #{@build.trigger_request.trigger.short_token}

          - if @build.trigger_request.variables
            %p
              %span.attr-name 变量：

            %code
              - @build.trigger_request.variables.each do |key, value|
                #{key}=#{value}

      .build-widget
        %h4.title
          提交
          .pull-right
            %small
              = link_to @build.commit.short_sha, ci_status_path(@build.commit), class: "monospace"
        %p
          %span.attr-name 分支：
          = link_to @build.ref, namespace_project_commits_path(@project.namespace, @project, @build.ref)
        %p
          %span.attr-name 作者：
          #{@build.commit.git_author_name}
        %p
          %span.attr-name 信息：
          #{@build.commit.git_commit_message}

      - if @build.tags.any?
        .build-widget
          %h4.title
            标签
          - @build.tag_list.each do |tag|
            %span.label.label-primary
              = tag

      - if @builds.present?
        .build-widget
          %h4.title 此提交还有 #{pluralize(@builds.count(:id), "次其他构建", "次其他构建")}
          = succeed "：" do
            = link_to @build.commit.short_sha, ci_status_path(@build.commit), class: "monospace"
          %table.table.builds
            - @builds.each_with_index do |build, i|
              %tr.build
                %td
                  = ci_icon_for_status(build.status)
                %td
                  = link_to namespace_project_build_path(@project.namespace, @project, build) do
                    - if build.name
                      = build.name
                    - else
                      %span ##{build.id}

                %td.status= ci_status_zh(build.status)


  :javascript
    new CiBuild("#{namespace_project_build_url(@project.namespace, @project, @build)}", "#{@build.status}", "#{trace_with_state[:state]}")
